<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                  |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                     |
// +------------------------------------------------------------------------+
// | Author: anguolei@clcw.com.cn                                           |
// +------------------------------------------------------------------------+
//
/**
 * 检测审核
 */

namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Employee;
use AuctionCore\CarResource\Car;
use AuctionCore\CarResource\CarList;
use AuctionCore\CarResource\CarSource;
use AuctionCore\ClcwUser\ClcwUser;

class DetectionCheckController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
        $auth = Employee::check_emp_auth("JianCeShenHe", $this->user_info['emp_id']);
        if (!$auth) {
            $this->helpJsRedirect("没有权限");
        }
    }

    public function index()
    {
        $this->display();
    }

    //审核列表
    public function checkList()
    {
        //$checkers = \AuctionCore\Account\Back\Employee::get_emp_by_group_or_store(382);
        $checkers = \AuctionCore\Account\Back\Employee::get_branch_emp(C('APPRAISER.0'), $this->user_info['branch_id']);
        $this->assign("checker", $checkers);
        $page = I("get.p/d", 1);
        $size = 10;
        $params = array();
        $params['status'] = I("get.status/d");
        $params['car_no'] = I("get.car_no");
        $params['start_time'] = I("get.start_time");
        $params['end_time'] = I("get.end_time");
        $params['audit_emp_name'] = I("get.audit_emp_name", '', 'trim');
        $params['checker_id'] = I("get.checker_id");
        $params['location_area'] = I("get.location_area");
        if (!$params['location_area']) {
            $params['location_area'] = !$this->is_hq ? array_flip($this->bc_city) : array();
        }
        //$params['composite_state'] = I("get.composite_state");
        //$params['accident_level'] = I("get.accident_level");
        $Car = new CarList();
        $result = $Car->check_list($page, $size, $params);
        $total = empty($result['total']) ? 0 : $result['total'];
        $list = empty($result['list']) ? array() : $result['list'];
        //分页
        $Page = new \AuctionCore\Common\AjaxPage($total, $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %downPage% %nextPage% %end% %jumpform%");
        $show = $Page->show(); // 分页显示输出
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('params', $params);
        $this->display();
    }

    //审核
    public function check()
    {
        $car_id = I("get.car_id/d");
        if ((int)$car_id < 1) {
            $this->helpJsRedirect("车辆编号错误");
        }
        $c = new Car($car_id);
        $carInfo = $c->detail();
        $carIds = $c->getCarIdsBySid($carInfo['sid']);
        $count = count($carIds);

        if (empty($carInfo)) {
            $this->helpJsRedirect("未查询到车辆信息");
        }
        $carInfo['transfer_limit'] = C('TRANSFER_LIMIT.' . $carInfo['transfer_limit']);
        $carInfo['suggest_price'] = format_money($carInfo['suggest_min_price']) . " - " . format_money($carInfo['suggest_max_price']);
        $carInfo['suggest_bid_start_price'] = format_money($carInfo['suggest_bid_start_price']);
        $carInfo['reserve_price'] = format_money($carInfo['reserve_price']);
        //协议详情
        $procedure_info = $c->detail('car_procedure');
        $procedure = $c->prefix_url($procedure_info);
        //获取车辆检测报告
        $Rep = A('CarReport');
        $report = $Rep->output_report($car_id);
        $this->assign('car_id', $car_id);
        $this->assign('total_car', $count);
        $this->assign('car_id_list', $carIds);
        $this->assign('report', $report);
        $this->assign('car', $carInfo);
        $this->assign('procedure', $procedure);
        $this->display();
    }

    //保存审核
    public function saveCheck()
    {
        $car_id = I("post.car_id/d");
        if ((int)$car_id < 1) {
            $this->helpJsonResult(411, "车辆编号错误");
        }
        $c = new Car($car_id);
        $carInfo = $c->detail();
        if ($carInfo['car_status'] > 1) {
            $this->helpJsonResult(500, "车辆已审核");
        }
        if (empty($carInfo)) {
            $this->helpJsonResult(412, "未查询到车辆信息");
        }
        $data = array();
        $status = I("post.status/d");
        $data['remark'] = I("post.remark", '');
        if ($status == 1) {
            $data['car_status'] = 4; //审核成功
            $branch_id = M("branch_city")->where("city_code=" . $carInfo['location_area'])->getField("branch_id");
            $conf = \AuctionCore\System\SystemConf::getConfByName("DEALER_PAY_MODE_" . $branch_id);
            $data['delivery_mode'] = $conf['value'];
            $msg = "审核成功";
            $owner = new \AuctionCore\CarOwner\Owner($carInfo['owner_id']);
            $ownerInfo = $owner->detail();
            $myCustomer = new \AuctionCore\Other\MyCustomer();
            $customer = $myCustomer->getCustomer(['owner_id' => $carInfo['owner_id'], 'status' => '0']);
            if (!empty($customer)) {
                if ($customer['phone'] == $ownerInfo['phone'] && $customer['name'] == $ownerInfo['seller_name']) {
                    $std_arr = ['status' => 1];
                } else {
                    $std_arr = ['status' => 2];
                }
                $myCustomer->updateById($customer['mc_id'], $std_arr);
            }
//            $channel = new \AuctionCore\Other\Channel($ownerInfo['comefrom']);
//            $channel_info = $channel->get_channel();

            if ($carInfo['car_source'] == 2) {
                D('ClcwUser','Logic')->createClcwUser($ownerInfo['owner_id'], $ownerInfo['phone']);
//                $owner->create_owner_user($ownerInfo['owner_id'], $ownerInfo['phone']);
            }
        } elseif ($status == 3) {
            $data['car_status'] = 3; //审核驳回
            if (empty($data['remark'])) {
                $this->helpJsonResult(413, "请填写驳回原因");
            }
            $msg = "审核驳回";
        } elseif ($status == 2) {
            //更新车源状态
            $CarSource = new CarSource($carInfo['sid']);
            $csData = [];
            $csData['status'] = 601;
            $res_cs = $CarSource->update($csData);

            $data['is_valid'] = 0; //报告失效
            $data['car_status'] = 2; //审核失败
            if (empty($data['remark'])) {
                $this->helpJsonResult(414, "请填写失败原因");
            }
            $msg = "审核失败";
        } else {
            $this->helpJsonResult(415, "状态错误");
        }

        if ($data['car_status'] == $carInfo['car_status']) {
            $this->helpJsonResult(416, "审核状态未更改");
        }
        $data['modify_time'] = date('Y-m-d H:i:s');
        $data['audit_emp_id'] = $this->user_info['emp_id'];//审核人
        $data['audit_time'] = date('Y-m-d H:i:s');
        $res = $c->update($data);
        if (!$res) {
            $this->helpJsonResult(500, "审核失败");
        }
        //记录员工操作日志
        $log = array(
            'emp_id' => $this->user_info['emp_id'],
            'order_id' => 0,
            'car_id' => $car_id,
            'owner_id' => 0,
            'action' => '车辆检测审核-' . $msg . "-" . $data['remark']
        );
        \AuctionCore\System\SystemLog::add($log);

        /**
         * 记录车辆跟踪日志
         */
        $car_action_data = array(
            'createtime' => date("Y-m-d H:i:s", time()),
            'action_name' => $msg,
            'emp_id' => $this->user_info['emp_id']
        );
        \AuctionCore\CarResource\Car::add_car_trace_log($car_id, $car_action_data);

        $this->helpJsonResult(200, "审核成功");
    }

}
